/* The MIT License (MIT)

	Copyright (c) 2013 Schpitt <schpitt@gmail.com>
	https://code.google.com/p/updator/

	Permission is hereby granted, free of charge, to any person obtaining a copy
	of this software and associated documentation files (the "Software"), to deal
	in the Software without restriction, including without limitation the rights
	to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
	copies of the Software, and to permit persons to whom the Software is
	furnished to do so, subject to the following conditions:

	The above copyright notice and this permission notice shall be included in
	all copies or substantial portions of the Software.

	THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
	IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
	FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
	AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
	LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
	OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
	THE SOFTWARE. */

#ifndef _MAIN_WINDOW_H_
#define _MAIN_WINDOW_H_

#include "IUpdateListener.h"

#include <queue>
#include <string>

#include <SFML/Graphics.hpp>

namespace sf {
	class RenderWindow;
}

class MainWindow : public Updator::IUpdateListener
{
	typedef enum {
		CONNECTED,
		DISCONNECTED,
		ADD_FILE_DOWNLOAD,
		BEGIN_FILE,
		END_FILE,
		DOWNLOAD,
		MESSAGE
	}   DisplayCommand;
	typedef struct {
		int size;
		char filename[1024];
	} BeginFileData;
	typedef union {
		unsigned int  simple_int;
		bool          simple_bool;
		BeginFileData begin_file;
	}  DataCommand;

	typedef struct {
		DisplayCommand cmd;
		DataCommand    data;
	} Command;

	std::queue<Command>	command_queue;

	sf::RenderWindow* window;
	sf::Font font;

	// display component
	sf::Sprite sprite_image_backgroung;
	sf::Texture image_backgroung;
	sf::Text title;
	sf::Text total_progress_text;
	sf::Text file_current_progress_text;
	sf::Text file_current_name;
	sf::Text file_number_text;
	sf::Text speed_text;
	sf::Text message_text;

	long total_size;
	long total_progress;
	long total_files_number;
	long total_files_number_progress;

	long  file_current_size;
	long  file_current_progress;
	
	float bytes_for_this_second;
	float second_counter;

	bool terminated;

	sf::Mutex mutex;

public:

	MainWindow( sf::RenderWindow* p_window );
	~MainWindow();

	void init();
	bool update( float p_frametime );
	bool isTerminated() { return terminated; }

	// IDownloadListener implementation
		virtual void connected( bool p_success );
		virtual void addToExecute( unsigned long p_size );
		virtual void executeProgress( unsigned int p_size );
		virtual void executeBegin( const char* p_filename, unsigned int p_size );
		virtual void executeEnd( bool p_success ); 
		virtual void updateEnd( bool p_ok ); 

		virtual void addToCheck() { }
		virtual void checkProgress() { }
		virtual void addToPostExecute() { }
		virtual void postExecuteProgress() { }

private:
	void refresh();
};

#endif
